package main.Web;
import com.alibaba.fastjson.JSON;
import main.Service.UserService;
import main.ServiceImpli.UserServiceImpl;
import main.pojo.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/16 11:01
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
@WebServlet("/CheckLogin")
public class CheckLogin extends HttpServlet {
    UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("来额");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        Map<String,Object> map=new HashMap<>();
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        User user1=new User();
//        user1.setUsername("Am126980");
//        user1.setPassword("12345");
        user1.setUsername(username);
        user1.setPassword(password);
        if (userService.CheckLogin(user1)){
            map.put("checklogin",true);
            System.out.println("登陆成功");
        }else {
            map.put("checklogin",false);
        }
        String json = JSON.toJSONString(map);
        PrintWriter pw = resp.getWriter();
        pw.print(json);
    }
}
